package org.zjsru.user.web;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.zjsru.user.pojo.PatternPropterties;
import org.zjsru.user.pojo.User;
import org.zjsru.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zjsru.user.util.JwtUtil;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
@RefreshScope
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户注册 - POST方式
     * @param user 用户信息
     * @return 注册成功的用户信息
     */
    @PostMapping("/register")
    public User register(@RequestBody User user) {
        log.info("用户注册(POST)：{}", user.getUsername());
        return userService.register(user);
    }

    /**
     * 用户注册 - GET方式
     * @param username 用户名
     * @param password 密码
     * @param phone 手机号
     * @param email 邮箱
     * @param address 地址
     * @return 注册成功的用户信息
     */
    @GetMapping("/register")
    public User registerByGet(
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "email", required = false) String email,
            @RequestParam(value = "address", required = false) String address) {
        log.info("用户注册(GET)：{}", username);
        
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setPhone(phone);
        user.setEmail(email);
        user.setAddress(address);
        
        return userService.register(user);
    }

    /**
     * 用户登录并返回JWT Token
     * @param username 用户名
     * @param password 密码
     * @return token
     */
    @PostMapping("/login")
    public Map<String, Object> loginWithToken(@RequestParam("username") String username,
                                              @RequestParam("password") String password) {
        log.info("用户登录：{}", username);
        Map<String, Object> result = new HashMap<>();
        try {
            User user = userService.login(username, password);
            // 登录成功，生成JWT Token
            String token = JwtUtil.generateToken(String.valueOf(user.getId()));
            result.put("token", token);
            result.put("user", user);
        } catch (RuntimeException e) {
            log.error("登录失败：{}", e.getMessage());
            result.put("error", e.getMessage());
        }
        return result;
    }

    /**
     * 检查用户状态
     * @param userId 用户ID
     * @return 用户状态信息
     */
    @GetMapping("/check-status")
    public User checkUserStatus(@RequestParam("userId") Long userId) {
        log.info("检查用户状态：{}", userId);
        User user = userService.queryById(userId);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        // 返回用户信息（不包含密码）
        user.setPassword(null);
        return user;
    }
}
